package com.eos.control.action;

import java.util.List;

import com.eos.business.service.SysUserBusinessService;
import com.eos.common.tool.ConstantTool;
import com.eos.common.tool.DataCacheUtil;
import com.eos.common.tool.ObjectUtils;
import com.eos.control.action.base.AbstractBaseAction;
import com.eos.orm.mybatis.model.SysRole;
import com.eos.orm.mybatis.model.SysUser;
import com.eos.orm.mybatis.model.ViewRoleMenu;
import com.hf.utils.MD5Util;
import com.hf.utils.StringUtil;

public class ProcessLoginAction extends AbstractBaseAction {
	private static final long serialVersionUID = -6811769024341921544L;
	private String email;
	private String password;
	private String validCode;
	private String message;
	private String menuJson;
	
	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getMessage() {
		return message;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getPassword() {
		return password;
	}

	public void setValidCode(String validCode) {
		this.validCode = validCode;
	}

	public String getMenuJson() {
		return menuJson;
	}

	@Override
	public String execute() throws Exception {
		try{
			if(StringUtil.isEmpty(email) || StringUtil.isEmpty(password)){
				message = "请输入邮箱或密码后再登录!";
				return LOGIN;
			}
			if(StringUtil.isEmpty(validCode)){
				message = "请输入验证码!";
				return LOGIN;
			}
			String code = (String) getSession().getAttribute("code");
			if(!validCode.equalsIgnoreCase(code)){
				message = "验证码输入错误!";
				return LOGIN;
			}else{
				SysUserBusinessService business = new SysUserBusinessService();
				SysUser user = new SysUser();
				user.setUserEmail(email);
				user.setPassWord(new MD5Util().encrypt(password));
				user.setDealFlag("0");
				List<SysUser> userList = business.findSysUserByCondition(user);
				if(userList != null && userList.size() >0){
					SysUser users = userList.get(0);
					if(users.getStatus().equals("1")){
						message = "登录失败,账号被冻结, 请联系管理员!";
					}else{
						getSession().setAttribute("SysUser", users);
						List<ViewRoleMenu> mapMenu = DataCacheUtil.getSysMenuOfRole(users.getRoleId());
						SysRole role = DataCacheUtil.getSysRole(users.getRoleId());
						if(null != role && role.getDealFlag().equals(ConstantTool.DEAL_FALG_OK) 
								&& role.getStatus().equals(ConstantTool.STATUS_OK)){
							menuJson = ObjectUtils.listToJson(mapMenu); //角色删除, 或无效时, 不查询此角色的菜单
						}
						return SUCCESS;
					}
				}else{
					message = "用户不存在!";
				}
			}
		}catch(Exception ex){
			ex.printStackTrace();
			message = "系统异常, 登录失败!";
		}
		return LOGIN;
	}
}
